package com.daisuxia.web.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daisuxia.back.dao.IChannelDynamicDao;
import com.daisuxia.constant.Constant;
import com.daisuxia.web.dao.IChannelInfoDao;
import com.daisuxia.web.dao.IChannelRateDao;
import com.daisuxia.web.dao.IChannelSuperInfoDao;
import com.daisuxia.web.dao.IPaginationDao;
import com.daisuxia.web.pojo.*;
import com.daisuxia.web.util.PageConfig;

@Service
public class ChannelInfoService implements IChannelInfoService {

	@Autowired
	private IChannelInfoDao channelInfoDao;
	@Autowired
	private IChannelSuperInfoDao channelSuperInfoDao;
	@Autowired
	private IPaginationDao paginationDao;
	@Autowired
	private IChannelRateDao channelRateDao;
	@Autowired
	private IChannelDynamicDao channelDynamicDao;

	@Override
	public List<ChannelInfo> findAll(Map<String, Object> params) {
		return channelInfoDao.findAll(params);
	}

	@Override
	public List<ChannelInfo> findAll2(Map<String, Object> params) {
		return channelInfoDao.findAll2(params);
	}

	public List<String> findAllChUser(Map<String, Object> params) {
		return channelInfoDao.findAllChUser(params);
	}
	@Override
	public ChannelInfo findOneChannelInfo(HashMap<String, Object> params) {
		List<ChannelInfo> list = this.findAll(params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ChannelInfo findOneChannelInfoNew(HashMap<String, Object> params) {
		List<ChannelInfo> list = this.findAll2(params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void insert(ChannelInfo channelInfo) {
		
		channelInfoDao.insert(channelInfo);
	}
	@Override
	public void insertChannelUserInfo(Map<String, Object> param) {
		channelInfoDao.insertChannelUserInfo(param);
	}

	@Override
	public void insertChannelDynamic(ChannelDynamic channelDynamic, User user) {
		channelDynamic.setCreateTime(new Date());
		channelDynamic.setApkUrl(user.getApkUrl());
		channelDynamic.setChannelTag(user.getChannelTag());
		channelDynamic.setToutiaoConvertId(user.getToutiaoConvertId());
		channelDynamic.setPicCodeNum(user.getPicCodeNum()	);
		channelDynamicDao.insertSelective(channelDynamic);
	}

	@Override
	public void updateById(ChannelInfo channelInfo) {
		channelInfoDao.updateById(channelInfo);
	}

	@Override
	public void deleteChannelInfoById(Integer id) {
		channelInfoDao.deleteById(id);
	}

	@Override
	public PageConfig<ChannelInfo> findPage(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelInfo");
		PageConfig<ChannelInfo> pageConfig = new PageConfig<ChannelInfo>();
		pageConfig = paginationDao.findPage("findAll", "findAllCount", params,"web");
		return pageConfig;

	}

	@Override
	public PageConfig<ChannelInfo> findChannelUserPage(
			HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelInfo");
		PageConfig<ChannelInfo> pageConfig = new PageConfig<ChannelInfo>();
		pageConfig = paginationDao.findPage("findUserAll", "findUserAllCount", params,"web");
		return pageConfig;
	}

	@Override
	public PageConfig<ChannelInfo> findChannelRecordPage(
			HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelInfo");
		PageConfig<ChannelInfo> pageConfig = new PageConfig<ChannelInfo>();
		pageConfig = paginationDao.findPage("findRecordAll", "findRecordAllCount", params,"web");
		return pageConfig;
	}

	@Override
	public ChannelInfo findChannelCode(String channelCode) {
		return channelInfoDao.findChannelCode(channelCode);
	}

	@Override
	public Integer findParamsCount(Map<String, Object> params) {
		return channelInfoDao.findRecordAllCount(params);
	}

	@Override
	public PageConfig<HashMap<String, Object>> findChannelUserBorrowRecord(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelInfo");
		PageConfig<HashMap<String, Object>> pageConfig = new PageConfig<HashMap<String, Object>>();
		pageConfig = paginationDao.findPage("findChanellBroowRecord", "findChanellBroowRecordCount", params,"web");
		return pageConfig;
	}

	@Override
	public HashMap<String, Object> countChanellUser(Integer channelId) {
		return channelInfoDao.countChanellUser(channelId);
	}
	
	
	/*************************添加推广渠道上级菜单**********************************/
	

	@Override
	public PageConfig<ChannelSuperInfo> findChannelSuperPage(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelSuperInfo");
		PageConfig<ChannelSuperInfo> pageConfig = new PageConfig<ChannelSuperInfo>();
		pageConfig = paginationDao.findPage("findAll", "findAllCount", params,"web");
		return pageConfig;

	}
	
	@Override
	public void insertChannelSuperInfo(ChannelSuperInfo channelSuperInfo) {
		channelSuperInfoDao.insert(channelSuperInfo);
	}
	
	@Override
	public void updateChannelSuperById(ChannelSuperInfo channelSuperInfo) {
		
		channelSuperInfoDao.updateById(channelSuperInfo);
	}
	
	@Override
	public List<ChannelSuperInfo> findSuperAll(Map<String, Object> params) {
		return channelSuperInfoDao.findAll(params);
	}
	@Override
	public List<ChannelSuperInfo> findSuperAll() {
		return channelSuperInfoDao.findAllNoParams();
	}
	@Override
	public ChannelSuperInfo findOneChannelSuperInfo(HashMap<String, Object> params) {
		List<ChannelSuperInfo> list = this.findSuperAll(params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**********************添加推广渠道费率菜单**************************/
	@Override
	public PageConfig<ChannelRate> findChannelRatePage(
			HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelRate");
		PageConfig<ChannelRate> pageConfig = new PageConfig<ChannelRate>();
		pageConfig = paginationDao.findPage("findAll", "findAllCount", params,"web");
		return pageConfig;
	}

	@Override
	public void insertChannelRate(ChannelRate channelRate) {
		channelRateDao.insert(channelRate);
	}

	@Override
	public void updateChannelRateById(ChannelRate channelRate) {
		channelRateDao.updateById(channelRate);
	}

	@Override
	public ChannelRate findOneChannelRateInfo(HashMap<String, Object> params) {
		List<ChannelRate> list = this.findChannelRateAll(params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ChannelRate> findChannelRateAll(Map<String, Object> params) {
		return channelRateDao.findAll(params);
	}
	@Override
	public List<ChannelRate> findChannelRateAll() {
		return channelRateDao.findAllNoParams();
	}

	@Override
	public List<ChannelInfo> findAllUserList(Map<String, Object> params) {
		return channelInfoDao.findAllUserList(params);
	}

	@Override
	public PageConfig<ChannelInfo> findUserAllPage(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "ChannelInfo");
		PageConfig<ChannelInfo> pageConfig = new PageConfig<ChannelInfo>();
		pageConfig = paginationDao.findPage("findUserAllPage", "findUserAllPageCount", params,"web");
		return pageConfig;
	}

	@Override
	public ChannelInfo findOneChannelInfoById(Integer id) {
		// TODO Auto-generated method stub
		return channelInfoDao.findById(id);
	}

	@Override
	public void deleteById(Integer id) {
		
		channelInfoDao.deleteById(id);
	}


}
